package com.cursordemo.service.impl;

import com.cursordemo.mapper.EmpExprMapper;
import com.cursordemo.mapper.EmpMapper;
import com.cursordemo.pojo.Emp;
import com.cursordemo.pojo.EmpExpr;
import com.cursordemo.pojo.PageBean;
import com.cursordemo.service.EmpService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.List;

/**
 * 员工Service实现类
 */
@Service
public class EmpServiceImpl implements EmpService {
    
    @Autowired
    private EmpMapper empMapper;
    
    @Autowired
    private EmpExprMapper empExprMapper;
    
    @Override
    public PageBean<Emp> list(String name, Integer gender, LocalDate begin, LocalDate end, Integer page, Integer pageSize) {
        // 设置分页参数
        PageHelper.startPage(page, pageSize);
        
        // 执行查询
        List<Emp> empList = empMapper.list(name, gender, begin, end);
        Page<Emp> p = (Page<Emp>) empList;
        
        // 为每个员工查询工作经历
        for (Emp emp : p.getResult()) {
            List<EmpExpr> exprList = empExprMapper.getByEmpId(emp.getId());
            emp.setExprList(exprList);
        }
        
        // 封装PageBean对象
        PageBean<Emp> pageBean = new PageBean<>(p.getTotal(), p.getResult());
        return pageBean;
    }
    
    @Override
    @Transactional
    public void delete(List<Integer> ids) {
        // 删除员工
        empMapper.delete(ids);
        
        // 删除员工相关的工作经历
        for (Integer id : ids) {
            empExprMapper.deleteByEmpId(id);
        }
    }
    
    @Override
    @Transactional
    public void add(Emp emp) {
        // 新增员工
        empMapper.insert(emp);
        
        // 新增工作经历
        if (emp.getExprList() != null && !emp.getExprList().isEmpty()) {
            for (EmpExpr expr : emp.getExprList()) {
                expr.setEmpId(emp.getId());
                empExprMapper.insert(expr);
            }
        }
    }
    
    @Override
    public Emp getById(Integer id) {
        // 查询员工基本信息
        Emp emp = empMapper.getById(id);
        
        // 查询员工工作经历
        if (emp != null) {
            List<EmpExpr> exprList = empExprMapper.getByEmpId(id);
            emp.setExprList(exprList);
        }
        
        return emp;
    }
    
    @Override
    @Transactional
    public void update(Emp emp) {
        // 更新员工基本信息
        empMapper.update(emp);
        
        // 删除原有工作经历
        empExprMapper.deleteByEmpId(emp.getId());
        
        // 新增工作经历
        if (emp.getExprList() != null && !emp.getExprList().isEmpty()) {
            for (EmpExpr expr : emp.getExprList()) {
                expr.setEmpId(emp.getId());
                empExprMapper.insert(expr);
            }
        }
    }
    
    @Override
    public List<Emp> listAll() {
        List<Emp> empList = empMapper.listAll();
        
        // 为每个员工查询工作经历
        for (Emp emp : empList) {
            List<EmpExpr> exprList = empExprMapper.getByEmpId(emp.getId());
            emp.setExprList(exprList);
        }
        
        return empList;
    }
}
